package com.hanhanxiaochong.zhongxiangchuanmei.view.home.act;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.NearByShopBean;
import com.hanhanxiaochong.zhongxiangchuanmei.config.page.PageRecorder;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.ChooseShopPresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ToastUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.home.adapter.NearbyShopAdapter;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IChooseShopView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import kr.co.namee.permissiongen.PermissionFail;
import kr.co.namee.permissiongen.PermissionGen;
import kr.co.namee.permissiongen.PermissionSuccess;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/5/2.
 * Desc   :
 */

public class ChooseShopActivity extends BaseActivity<ChooseShopPresenter> implements IChooseShopView, AMapLocationListener {

    @BindView(R.id.ll_topleft)
    LinearLayout llTopleft;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smartRefresh;

    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    //声明AMapLocationClientOption对象
    public AMapLocationClientOption mLocationOption = null;
    private PageRecorder recorder = new PageRecorder();
    private double latitude;
    private double longitude;
    private String id;
    private String type;
    private String cityCode;
    private NearbyShopAdapter adapter;

    public static Intent createIntent(Context context, String id, String type) {
        Intent intent = new Intent(context, ChooseShopActivity.class);
        intent.putExtra("id", id);
        intent.putExtra("type", type);
        return intent;
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_choose_shop);
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        type = intent.getStringExtra("type");
    }

    @Override
    protected void initPresenter() {
        mPresenter = new ChooseShopPresenter(this);
    }

    @Override
    protected void init() {
        //6.0以上权限获取
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) permissionCheck();
        smartRefresh.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                mPresenter.getList(id, cityCode, type, latitude, longitude, recorder.getCurrentPage());
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                recorder.moveToFirstPage();
                mPresenter.getList(id, cityCode, type, latitude, longitude, recorder.getCurrentPage());
            }
        });
        List<NearByShopBean> list = new ArrayList<>();
        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new NearbyShopAdapter(mContext, R.layout.item_nearby_shop, list);
        recyclerview.setAdapter(adapter);
        adapter.setmOnItemClickListener((arg0, arg1) ->
                startActivity(AppointmentActivity.createIntent(mContext, adapter.getDatas().get(arg1).getId(), id)));
    }

    private void permissionCheck() {
        PermissionGen.with(this)
                .addRequestCode(100)
                .permissions(
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION)
                .request();
    }

    @PermissionFail(requestCode = 100)
    public void doFailSomething() {
        ToastUtils.showToast("权限获取失败,无法获取门店列表");
        finish();
    }

    @PermissionSuccess(requestCode = 100)
    public void success() {
        initLocation();
    }

    private void initLocation() {
        //初始化定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
        //设置定位回调监听
        mLocationClient.setLocationListener(this);
        //初始化AMapLocationClientOption对象
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        mLocationOption.setOnceLocation(true);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();
    }

    @OnClick(R.id.ll_topleft)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void setData(List<NearByShopBean> data) {
        adapter.setDatas(data);
        recorder.moveToNextPage();
    }

    @Override
    public void addData(List<NearByShopBean> data) {
        adapter.addDatas(data);
        recorder.moveToNextPage();
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
//        cityCode = aMapLocation.getCityCode();
        cityCode = "110100";
        latitude = aMapLocation.getLatitude();
        longitude = aMapLocation.getLongitude();
        mPresenter.getList(id, cityCode, type, latitude, longitude, recorder.getCurrentPage());
    }
}
